package com.mccree.order.controller;

import cn.hutool.json.JSONUtil;
import com.mccree.cloud.common.R;
import com.mccree.cloud.dao.entity.PaymentEntity;
import com.mccree.order.constants.URIConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author: MccRee
 * @Date: 2022/6/2 12:31
 */
@RestController
@Slf4j
@RequestMapping("api/order")
@Api(tags = "订单模块")
public class OrderController {

    public static void main(String[] args) {
        String msg = "{\"name\":\"cloud-provider-payment\",\"id\":\"c118f463-f3a8-4c15-8e33-b6aceb10a5b6\",\"address\":\"LAPTOP-N74OK91G\",\"port\":8004,\"sslPort\":null,\"payload\":{\"@class\":\"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance\",\"id\":\"application-1\",\"name\":\"cloud-provider-payment\",\"metadata\":{}},\"registrationTimeUTC\":1654741590686,\"serviceType\":\"DYNAMIC\",\"uriSpec\":{\"parts\":[{\"value\":\"scheme\",\"variable\":true},{\"value\":\"://\",\"variable\":false},{\"value\":\"address\",\"variable\":true},{\"value\":\":\",\"variable\":false},{\"value\":\"port\",\"variable\":true}]}";
        String jsonStr = JSONUtil.formatJsonStr(msg);
        System.out.println(jsonStr);
    }


    @Resource
    private RestTemplate restTemplate;

    /**
     * 创建支付订单
     * @param paymentEntity
     * @return
     */
    @ApiOperation("创建支付订单")
    @PostMapping("/create")
    public R create(@RequestBody PaymentEntity paymentEntity){
        try {
            return restTemplate.postForObject(URIConstants.PAYMENT_URL+"/sys/pay/create",paymentEntity,R.class);
        }catch (Exception e){
            log.error("ORDER_CREATE_ERROR:",e);
            return R.error(e.getMessage());
        }
    }

    @ApiOperation("获取所有数据")
    @GetMapping("/getAll")
    public R<PaymentEntity> getAll(){
        try {
            return restTemplate.getForObject(URIConstants.PAYMENT_URL + "/sys/pay/getAll", R.class);
        }catch (Exception e){
            log.error("ORDER_GET_ALL_ERROR:",e);
            return R.error(e.getMessage());
        }
    }
}
